﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using BloomFilter;

namespace BloomFilter.String
{
    public class StringBloomFilter : BloomFilter<string>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableSize">哈希数组大小，大于0的整数</param>
        /// <param name="nKeys">关键值个数</param>
        /// <param name="haveNullOrEmptyString">是否允许含有 null 和 空字符串</param>
        public StringBloomFilter(int tableSize, int nKeys)
            : base(tableSize, nKeys) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hashTable">现有哈希数组</param>
        /// <param name="nKeys">关键值个数</param>
        /// <param name="haveNullOrEmptyString">是否允许含有 null 和 空字符串</param>
        public StringBloomFilter(BitArray hashTable, int nKeys)
            : base(hashTable, nKeys) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hashTable"></param>
        /// <param name="nKeys"></param>
        public StringBloomFilter(byte[] hashTable, int nKeys)
            : base(hashTable, nKeys) { }


        protected override int CreateHash_1(string val)
        {
            return val.GetHashCode();
        }

        protected override int CreateHash_2(string val)
        {
            int hash = 0;
            for (int i = 0; i < val.Length; i++)
            {
                hash += val[i];
                hash += (hash << 10);
                hash ^= (hash >> 6);
            }
            hash += (hash << 3);
            hash ^= (hash >> 11);
            hash += (hash << 15);
            return hash;
        }

        protected override bool IsValid(string val)
        {
            return val != null;
        }
    }
}
